﻿using Back_end.Models;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Description;

namespace Back_end.APIControllers
{
	// [Authorize]
	[RoutePrefix("api/News")]
	[EnableCors("*", "*", "*")]
	public class NewsApiController : ApiController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: api/News
		[Route("GetNews")]
        public IQueryable<News> GetNews()
        {
            return db.News.OrderBy(n => n.PublishedDate);
        }

		// GET: api/News/5
		[ResponseType(typeof(News))]
		[Route("GetById")]
        public async Task<IHttpActionResult> GetNews(string id)
        {
            News news = await db.News.FindAsync(id);
            if (news == null)
            {
				return NotFound();
            }

            return Ok(news);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool NewsExists(string id)
        {
            return db.News.Count(e => e.NewsId == id) > 0;
        }
    }
}